package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Follow;
import cn.wolfcode.trip.base.service.IFollowService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 关注
 */
@RestController
@RequestMapping("/follows")
public class FollowController {

    @Autowired
    private IFollowService followService;

    /**
     * 添加用户关注
     * 1: 资源 /follows
     * 2: 请求方式 post
     * 3: 请求参数 Follow对象
     * 4: 返回结果 jsonResult
     */
    @PostMapping
    public JsonResult addFollow(Follow follow){
        JsonResult result = new JsonResult();
        try {
            followService.addFollow(follow);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 取消关注
     * 1 资源 /follows
     * 2 请求方式 put
     * 3 请求参数 fansId
     * 4 返回结果 jsonResult
     */
    @PutMapping
    public JsonResult cancelFollow(Follow follow){
        JsonResult result = new JsonResult();
        try {
            followService.cancelFollow(follow);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 查询用户是否已经关注了指定的用户
     * 1 资源 /follows/relations
     * 2 请求方式 get
     * 3 请求参数 Follow对象
     * 4 返回结果 JsonResult对象
     */
    @GetMapping("/relations")
    public JsonResult listRelations(Follow follow){
        JsonResult result = new JsonResult();
        try {
            Follow relations = followService.listRelations(follow);
            result.setData(relations);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}